<?php
/**
 * Created by PhpStorm.
 * User: wangsentao
 * Date: 2018/3/2
 * Time: 下午3:04
 */

namespace app\api\repository;


use app\common\Service;

class OrderInfoPayRepository extends Service
{
    /**
     * 创建支付订单 详细记录
     */
    public function createPayOrder($uid, $order_id,$order_num, $pay_type, $amount)
    {
        $model = model("app\common\Model\OrderInfoPay");
        $model->uid = $uid;
        $model->order_id = $order_id;
        $model->status = 1;
        $model->order_num = $order_num;
        $model->pay_type = $pay_type;
        $model->amount = $amount;
        $model->trade_no = '';

        if ($model->save()) {
            return $model->getData();
        }

        return $model->getError();
    }

    /**
     * @param null $order_num
     * @param null $order_id
     * @param string $trade_no
     * @return array
     */
    public function updateOrderPayedStatus($order_num = null, $order_id = null,$trade_no = '')
    {
        if ($order_num == null && $order_id == null) {
            return [
                'code' => '100031',
                'msg' => '订单号或订单ID只允许一个为空',
            ];
        }

        $model = model("app\common\Model\OrderInfoPay");

        if ($order_num == null) {
            $model->where('order_id', $order_id);
        }

        if ($order_id == null) {
            $model->where('order_num', $order_num);
        }

        if ($trade_no != '') {
            $model->trade_no = $trade_no;
        }
        $model->status = 2;

        return [
            'code' => '000000',
            'data' => $model->isUpdate(true)->save(),
        ];
    }

    /**
     * 根据订单号获取订单信息
     */
    public function getOrderInfoById($order_id)
    {
        if (empty($order_id)) {
            return [
                'code' => '100031',
                'msg' => '订单id不能为空',
            ];
        }
        $model = model("app\common\Model\OrderInfoPay");
        $res = $model
            ->field(
                'uid,
                      order_num,
                      amount,
                      trade_no,
                      status')
            ->where('order_id',$order_id)
            ->find();
        return [
            'code' => '000000',
            'data' => $res->getData(),
        ];
    }
}